package com.jinlejia.bbs.ui.message;

import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.jinlejia.bbs.R;
import com.jinlejia.bbs.adapter.message.AnswerAdapter;
import com.jinlejia.bbs.bean.CommentBean;
import com.jinlejia.bbs.databinding.BbsActivityLikeListBinding;
import com.jinlejia.bbs.help.CommonEvent;
import com.jinlejia.bbs.logic.message.MessageLikeListLogic;
import com.jinlejia.bbs.widget.mp3.AudioController;
import com.jinlejia.bbs.widget.recycler.PullInterface;

import java.util.ArrayList;

/**
 * 获赞列表
 */
public class BBSLikeListActivity extends AppCompatActivity implements MessageLikeListLogic.LogicCallBack, SwipeRefreshLayout.OnRefreshListener, PullInterface.LoadMoreListener {
    private BbsActivityLikeListBinding binding;

    private MessageLikeListLogic logic;
    private ArrayList<CommentBean> dataList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.bbs_activity_like_list);
        binding.setCommonEvent(CommonEvent.obtain());
        dataList = new ArrayList<>();
        logic = new MessageLikeListLogic(this,dataList,this);
        initView();
    }
    private AudioController audioController;
    private AnswerAdapter adapter;
    private void initView() {
        audioController = new AudioController(this);
        binding.swipeRefreshLayout.setColorSchemeResources(R.color.holo_blue_bright, R.color.holo_green_light,
                R.color.holo_orange_light, R.color.holo_red_light);
        binding.swipeRefreshLayout.setOnRefreshListener(this);

        adapter = new AnswerAdapter(this,audioController,binding.recyclerView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        binding.recyclerView.setAdapter(adapter);
        binding.recyclerView.setLayoutManager(layoutManager);

        binding.swipeRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                binding.swipeRefreshLayout.setRefreshing(true);
                logic.refresh();
            }
        });
    }

    @Override
    protected void onDestroy() {
        audioController.release();
        super.onDestroy();
    }

    @Override
    public void onResult(boolean hasNextPage) {
        adapter.initData(dataList);
        if (!hasNextPage){
            binding.recyclerView.setAutoLoadMoreListener(null);
        }else {
            binding.recyclerView.setAutoLoadMoreListener(this);
        }
        binding.swipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void onLoadMore() {
        binding.swipeRefreshLayout.setRefreshing(true);
        logic.load();
    }

    @Override
    public void onRefresh() {
        binding.swipeRefreshLayout.setRefreshing(true);
        logic.refresh();
    }
}
